const router = require('express').Router();
const db = require('./../config/dbcofnig')
const upload = require("./../config/uploadconfig")

// 多条件分页查询
router.route('/searchDoctors').get((req, res) => {
  let { dname, jid, dsex, nowpage, pagesize } = req.query;
  console.log(dname, jid, typeof dsex)
  if (!dname && !jid && !dsex) {
    res.json({
      code: 4001, msg: '查询，搜索要给条件参数哦。'
    })
    return;
  }

  let sql = `SELECT * FROM doctorstable AS d INNER JOIN jobs AS j ON d.jid = j.jid WHERE 1=1`;
  // 分页时，查满足条件的总条数
  let sql2 = `SELECT count(*) as count FROM doctorstable AS d INNER JOIN jobs AS j ON d.jid = j.jid WHERE 1=1`;
  let params = [];
  let params2 = []
  if (!!dname) {
    // sql += " and dname = ?"  // 等值判断，不是模糊查询
    // params.push( dname )
    sql += " and dname like ?"  // 模糊查询
    params.push("%" + dname + "%")
    sql2 += " and dname like ?"  // 总条数：模糊查询
    params2.push("%" + dname + "%")
  }

  if (!!jid) {
    sql += " and d.jid = ?"
    params.push(Number(jid))
    sql2 += " and d.jid = ?" // 总条数：
    params2.push(Number(jid))
  }

  if (!!dsex) {
    sql += " and dsex = ?"
    params.push(dsex)
    sql2 += " and dsex = ?" // 总条数：
    params2.push(dsex)
  }
  sql2 += ";";

  // 分页
  let startIndex = (nowpage - 1) * pagesize;
  pagesize = Number(pagesize)
  sql += ` limit ?,?;`
  params.push(startIndex)
  params.push(pagesize)


  let cb = (err, data) => {
    console.log(err)
    if (err === null) {
      res.json({
        code: 200, msg: '查询成功',
        data: data[0],
        count: data[1][0].count
      })
    } else {
      res.json({
        code: 4002, msg: '查询失败', data: []
      })
    }
    console.log(sql + sql2)
    console.log(params.concat(params2))

  }

  db.connect(sql + sql2, params.concat(params2), cb);
});

// 添加医生
// dphoto要和formdata对象里面的键名一致！！
router.route("/addDoctor").post(upload.single('dphoto'), (req, res) => {
  let { dname, dsid, dsex, dfeaturs, jid, dtel } = req.body;
  let dphoto = req.file?.filename; //可选链运算符（?.）
  console.log(dname, dsid, dsex, dfeaturs, jid, dphoto)
  if (!dtel || !dname || !dsid || !dsex || !dfeaturs || !jid || !dphoto) {
    res.json({
      code: 4001, msg: '参数不全'
    })
    return;
  }

  let sql = `insert into doctorstable values(null,?,?,?,?,?,?,?);`;
  let params = [dname, dsex, dsid, dtel, dfeaturs, jid, dphoto] // 参数顺序和mysql列的先后一致
  let cb = (err, data) => {
    console.log(err);
    if (err === null) {
      res.json({
        code: 200, msg: '添加成功'
      })
    } else {
      res.json({
        code: 4002, msg: '添加失败'
      })
    }
  }
  db.connect(sql, params, cb)


})

// 获取一页医生数据
router.route("/getDoctors").get((req, res) => {
  let { pagesize, nowpage } = req.query;
  let sql2 = `select count(*) as count from doctorstable;`
  let sql = `select * from doctorstable limit ?,?;`;
  let startIndex = (nowpage - 1) * pagesize;
  let params = [startIndex, Number(pagesize)];
  let cb = (err, data) => {
    console.log(err);
    if (err == null) {
      res.json({
        code: 200, msg: '查询成功', data: data[0], count: data[1][0].count
      })
    } else {
      res.json({
        code: 4002, msg: '查询失败', data: []
      })
    }
  }
  db.connect(sql + sql2, params, cb)
});

module.exports = router;